package com.kuaimacode.kframework.agent.controller.holiday;

import com.github.pagehelper.PageInfo;
import com.kuaimacode.kframework.agent.services.holiday.HolidayService;
import com.kuaimacode.kframework.agent.util.SessionUtil;
import com.kuaimacode.kframework.common.Constant;
import com.kuaimacode.kframework.common.JsonResult;
import com.kuaimacode.kframework.constants.PageConstants;
import com.kuaimacode.kframework.mybatis.models.holiday.Holiday;
import com.kuaimacode.kframework.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author anzy
 * @date 2019/06/14
 */

/**
 * Controller
 */
@Controller
@RequestMapping({"/holiday"})
public class HolidayController {
	private static String PAGE_FILE_NAME = "holiday/";

	@Autowired
	private HolidayService holidayServices;




	@RequestMapping(value = {"/index"})
	public String index(@RequestParam(value = "pageNum", required = true, defaultValue = PageConstants.PAGE_NUM+"") int pageNum,
										@RequestParam(value = "pageSize", required = true, defaultValue = PageConstants.PAGE_SIZE+"") int pageSize,
						ModelMap model) {
		PageInfo<Holiday> pageInfo = holidayServices.findAll(pageNum, pageSize);
		// pageInfo.list holiday
		model.put("pageInfo", pageInfo);
		return PAGE_FILE_NAME + "index";
	}


	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add(ModelMap map) {
		Holiday holiday = new Holiday();
		map.put("holiday", holiday);
		map.put("isAdd", true);
		return PAGE_FILE_NAME+"form";
	}

	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	public String edit(@PathVariable String id, ModelMap map) {
		Holiday holiday = holidayServices.getById(id);
		map.put("holiday", holiday);
		map.put("isAdd", false);
		return PAGE_FILE_NAME+"form";
	}

	@RequestMapping(value = {"/edit"}, method = RequestMethod.POST)
	@ResponseBody
	public JsonResult edit(Holiday holiday, Boolean isAdd,ModelMap map) {
		try {
			if(isAdd){
				// 新建保存
				//判断是否有已经存在的生效的节假日数据
				List<Holiday> list = holidayServices.getIsRunning(SessionUtil.getAgentId(),
						DateUtil.format(holiday.getBeginDate(),DateUtil.LONG_DATE_FORMAT),
						DateUtil.format(holiday.getEndDate(),DateUtil.LONG_DATE_FORMAT));
				if(list !=null && list.size()>0) return JsonResult.failure("存在与当前日期重叠的节假日配置，请先删除后在操作");
				holiday.setAgentId(SessionUtil.getAgentId());
				holiday.setCreateAt(new Date());
				holiday.setCreateBy(SessionUtil.getUserId());
				holiday.setStatus(Constant.DeleteStateEnum.NORMAL.getCode());
				holidayServices.save(holiday);
			}else{
				// 编辑保存
				holidayServices.updateById(holiday);
			}
		} catch (Exception e) {
			return JsonResult.failure(e.getMessage());
		}
		return JsonResult.success();
	}


	@RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
	public String detail(@PathVariable String id, ModelMap map) {
		Holiday holiday = holidayServices.getById(id);
		map.put("holiday", holiday);
		return PAGE_FILE_NAME+"detail";
	}


	@ResponseBody
	@PostMapping(value = "/delete/{id}")
	public JsonResult delete(@PathVariable String id, ModelMap map) {
		try {
			Holiday holiday = holidayServices.getById(id);
			holiday.setStatus(Constant.DeleteStateEnum.DELETE.getCode());
			holidayServices.updateById(holiday);
		} catch (Exception e) {
			return JsonResult.failure(e.getMessage());
		}
		return JsonResult.success();
	}


}
